local xianshi = fk.CreateSkill{
  name = "rmt__xianshi",
}

xianshi:addEffect(fk.TurnStart, {
  anim_type = "control",
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(xianshi.name) and not target.dead then
      return #player:getTableMark("rmt__xianshi_choice-round") < 2
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = xianshi.name,
      prompt = "#rmt__xianshi-invoke:"..target.id
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = 3
    if player:getMark("rmt__xianshi_noboubling-round") == 0 then
      n = 2 * n
    end
    local cards = room:getNCards(n)
    local all_choices = {"rmt__xianshi_show", "rmt__xianshi_discard"}
    local used = player:getTableMark("rmt__xianshi_choice-round")
    local choices = table.filter(all_choices, function (name) return not table.contains(used, name) end)
    local choice = room:askToViewCardsAndChoice(player, { cards = cards, choices = choices, skill_name = xianshi.name})
    room:addTableMark(player, "rmt__xianshi_choice-round", choice)
    if choice == "rmt__xianshi_show" then
      local give_cards = room:askToPoxi(player, {
        poxi_type = "rmt__xianshi",
        data = {
          { "Top", cards },
        },
        cancelable = false
      })
      player:showCards(give_cards)
      room:obtainCard(target, give_cards, true, fk.ReasonJustMove, target.id, xianshi.name)
      player.tag["rmt__xianshi_give"] = true
    else
      cards = room:askToChooseCards(player, {
        target = player,
        min = 1,
        max = #cards,
        flag = { card_data = { { "弃置", cards } } },
        skill_name = xianshi.name,
        prompt = "#rmt__xianshi-discard"
      })
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, xianshi.name)
    end
  end,
})

xianshi:addEffect(fk.RoundStart, {
  can_refresh = function (self, event, target, player, data)
    return player.tag["rmt__xianshi_give"]
  end,
  on_refresh = function (self, event, target, player, data)
    player.tag["rmt__xianshi_give"] = nil
    player.room:setPlayerMark(player, "rmt__xianshi_noboubling-round", 1)
  end,
})

Fk:addPoxiMethod{
  name = "rmt__xianshi",
  card_filter = function(to_select, selected, data, extra_data)
    if #selected == 0 then return true end
    return Fk:getCardById(to_select).color == Fk:getCardById(selected[1]).color
  end,
  feasible = function(selected, data, extra_data)
    return #selected > 0
  end,
  default_choice = function (data, extra_data)
    if data == nil then return {} end
    return {data[1][2][1]}
  end,
}

Fk:loadTranslationTable{
  ["rmt__xianshi"] = "先识",
  [":rmt__xianshi"] = "一名角色的回合开始时，你可以观看牌堆顶的三张牌（若你上轮发动“先识”没有角色获得牌，则翻倍），然后执行以下本轮未执行过的一项：展示其中任意数量相同颜色的牌，其获得之；将其中任意数量的牌置入弃牌堆。",
  ["#rmt__xianshi-invoke"] = "先识：“%src”的回合，你可以观看牌堆顶牌并选一项",
  ["rmt__xianshi_show"] = "展示同色牌",
  ["rmt__xianshi_discard"] = "置入弃牌堆",
  ["#rmt__xianshi-discard"] = "选择牌置入弃牌堆",

  ["$rmt__xianshi1"] = "行略于先，未雨绸缪。",
  ["$rmt__xianshi2"] = "先见梧叶，而后知秋。",
}

return xianshi
